package com.ygqh.baby.controller.admin;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.po.YgSupplier;
import com.ygqh.baby.po.YgUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.SystemInfo;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgSupplierService;
import com.ygqh.baby.shiro.utils.SessionUtil;

public class BaseController<Entity,PK> {
    //** 错误消息 *//*
	protected static final Message ERROR_MESSAGE = Message.error("操作失败");
	//** 成功消息 *//*
	protected static final Message SUCCESS_MESSAGE = Message.success("操作成功");
	@Autowired
	protected SystemInfo systemInfo;
	@Autowired
	private YgSupplierService ygSupplierService;
	@Autowired
	private RedisDao redisDao;
	
	public YgUser getSession(){
		return SessionUtil.getCurrentUser();
		
	}
	public YgSupplier getSupplier() {
		// 获取当前用户的 未发货订单
		String userName = getSession().getUserName();
		String s=redisDao.get("supplier_"+userName);
		YgSupplier supplier=new YgSupplier();		
		if(s==null){
			supplier=ygSupplierService.findSupplierId(userName);
			redisDao.set("supplier_"+userName,JSON.toJSONString(supplier),0);
		}else{
			supplier=JSON.parseObject(s,YgSupplier.class);
		}
		
		return supplier;

	}
	
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
		dateFormat.setLenient(false);  
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值 
	}
	
}
